package org.jeecg.modules.lab.items.temp2.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.cronJob.MyEnum;
import org.jeecg.common.cronJob.entity.LabCronJob;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.lab.cronJob.service.ILabCronJobService;
import org.jeecg.modules.lab.items.temp2.entity.LabItemsTemp2Target;
import org.jeecg.modules.lab.items.temp2.service.ILabItemsTemp2TargetService;
import org.jeecg.modules.lab.items.vo.LabItemsTargetPage;
import org.jeecg.modules.lab.items.vo.LabItemsTargetVo;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description: 实验项目模板-检测指标
 * @Author: jeecg-boot
 * @Date: 2023-11-15
 * @Version: V1.0
 */
@Api(tags = "实验项目模板-检测指标")
@RestController
@RequestMapping("/items/labItemsTemp2Target")
@Slf4j
public class LabItemsTemp2TargetController extends JeecgController<LabItemsTemp2Target, ILabItemsTemp2TargetService> {
    @Autowired
    private ILabItemsTemp2TargetService labItemsTemp2TargetService;
    @Autowired
    private ILabCronJobService labCronJobService;

    /**
     * 分页列表查询
     *
     * @param labItemsTemp2Target
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "实验项目模板-检测指标-分页列表查询")
    @ApiOperation(value = "实验项目模板-检测指标-分页列表查询", notes = "实验项目模板-检测指标-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<LabItemsTemp2Target>> queryPageList(LabItemsTemp2Target labItemsTemp2Target,
                                                            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                            @RequestParam(name = "pageSize", defaultValue = "50") Integer pageSize,
                                                            @NotNull HttpServletRequest req) {
        QueryWrapper<LabItemsTemp2Target> queryWrapper = QueryGenerator.initQueryWrapper(labItemsTemp2Target, req.getParameterMap());
        Page<LabItemsTemp2Target> page = new Page(pageNo, pageSize);
        IPage<LabItemsTemp2Target> pageList = labItemsTemp2TargetService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param labItemsTemp2Target
     * @return
     */
    @AutoLog(value = "实验项目模板-检测指标-添加")
    @ApiOperation(value = "实验项目模板-检测指标-添加", notes = "实验项目模板-检测指标-添加")
//	@RequiresPermissions("target:lab_items_temp2_target:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody LabItemsTemp2Target labItemsTemp2Target) {
        labItemsTemp2TargetService.save(labItemsTemp2Target);

        if (MyEnum.sync){
            long time = new Date().getTime();
            LabCronJob job = new LabCronJob("lab_items_temp2_target","0",time,1);
            labCronJobService.save(job);
        }
        return Result.OK("添加成功！");
    }

    /**
     * 添加指标
     *
     * @param labItemsTargetPage
     * @return
     */
    @AutoLog(value = "实验项目模板-添加指标")
    @ApiOperation(value = "实验项目模板-添加指标", notes = "实验项目模板-添加指标")
    @PostMapping(value = "/addTarget")
    public Result<String> addTarget(@RequestBody LabItemsTargetPage labItemsTargetPage) {
        String itemsId = labItemsTargetPage.getItemsId();
        //查询已有指标
        QueryWrapper<LabItemsTemp2Target> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("items_id", itemsId);
        List<LabItemsTemp2Target> labItemsTemp2TargetList = labItemsTemp2TargetService.list(queryWrapper);
        int num = labItemsTemp2TargetList.size();
        List<LabItemsTargetVo> itemsTargetList = labItemsTargetPage.getItemsTargetList();

        if (num > 0) {
            for (int i = 0; i < itemsTargetList.size(); i++) {
                LabItemsTargetVo targetVo = itemsTargetList.get(i);
                boolean bool = true;
                for (LabItemsTemp2Target target : labItemsTemp2TargetList) {
                    if (targetVo.getFieldName().equals(target.getFieldName())) {
                        bool = false;
                        break;
                    }
                }
                if (bool) {
                    LabItemsTemp2Target target = new LabItemsTemp2Target();
                    target.setItemsId(itemsId);
                    target.setFieldName(targetVo.getFieldName());
                    target.setFieldCode(targetVo.getFieldCode());
                    target.setFieldType(targetVo.getFieldType());
                    target.setFieldSort(num + i + 1);
                    labItemsTemp2TargetService.save(target);
                }
            }
        } else {
            for (int i = 0; i < itemsTargetList.size(); i++) {
                LabItemsTargetVo targetVo = itemsTargetList.get(i);
                LabItemsTemp2Target target = new LabItemsTemp2Target();
                target.setItemsId(itemsId);
                target.setFieldName(targetVo.getFieldName());
                target.setFieldCode(targetVo.getFieldCode());
                target.setFieldType(targetVo.getFieldType());
                target.setFieldSort(i + 1);
                labItemsTemp2TargetService.save(target);
            }
        }
        if (MyEnum.sync){
            long time = new Date().getTime();
            LabCronJob job = new LabCronJob("lab_items_temp2_target","0",time,1);
            labCronJobService.save(job);
        }
        return Result.OK("指标添加成功！");
    }

    /**
     * 编辑
     *
     * @param labItemsTemp2Target
     * @return
     */
    @AutoLog(value = "实验项目模板-检测指标-编辑")
    @ApiOperation(value = "实验项目模板-检测指标-编辑", notes = "实验项目模板-检测指标-编辑")
//	@RequiresPermissions("target:lab_items_temp2_target:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody LabItemsTemp2Target labItemsTemp2Target) {
        labItemsTemp2Target.setSynFlag("1");
        labItemsTemp2TargetService.updateById(labItemsTemp2Target);

        if (MyEnum.sync){
            long time = new Date().getTime();
            LabCronJob job = new LabCronJob("lab_items_temp2_target","1",time,1);
            labCronJobService.save(job);
        }
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "实验项目模板-检测指标-通过id删除")
    @ApiOperation(value = "实验项目模板-检测指标-通过id删除", notes = "实验项目模板-检测指标-通过id删除")
//	@RequiresPermissions("target:lab_items_temp2_target:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        labItemsTemp2TargetService.removeById(id);

        if (MyEnum.sync){
            long time = new Date().getTime();
            String condition="id='"+id+"'";
            LabCronJob job = new LabCronJob("lab_items_temp2_target","2",condition,time,1);
            labCronJobService.save(job);
        }
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "实验项目模板-检测指标-批量删除")
    @ApiOperation(value = "实验项目模板-检测指标-批量删除", notes = "实验项目模板-检测指标-批量删除")
//	@RequiresPermissions("target:lab_items_temp2_target:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.labItemsTemp2TargetService.removeByIds(Arrays.asList(ids.split(",")));

        if (MyEnum.sync) {
            long time = new Date().getTime();
            String[] split = ids.split(",");
            labCronJobService.saveCronBatch("lab_items_temp2_target","2",time,split);
        }
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "实验项目模板-检测指标-通过id查询")
    @ApiOperation(value = "实验项目模板-检测指标-通过id查询", notes = "实验项目模板-检测指标-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<LabItemsTemp2Target> queryById(@RequestParam(name = "id", required = true) String id) {
        LabItemsTemp2Target labItemsTemp2Target = labItemsTemp2TargetService.getById(id);
        if (labItemsTemp2Target == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(labItemsTemp2Target);
    }


    /**
     * 导出excel
     *
     * @param request
     * @param labItemsTemp2Target
     */
    @RequiresPermissions("target:lab_items_temp2_target:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LabItemsTemp2Target labItemsTemp2Target) {
        return super.exportXls(request, labItemsTemp2Target, LabItemsTemp2Target.class, "实验项目模板-检测指标");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequiresPermissions("target:lab_items_temp2_target:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, LabItemsTemp2Target.class);
    }

}
